package com.linciping.useges.plugin.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.linciping.useges.plugin.asyc.ModuleResUsegesThread;
import com.linciping.useges.plugin.bean.File2R;
import com.linciping.useges.plugin.util.Util;
import com.linciping.useges.plugin.view.BackgroundProgressWindow;
import org.jetbrains.jps.model.java.JavaResourceRootType;
import org.jetbrains.jps.model.java.JavaSourceRootType;
import org.jetbrains.jps.model.module.JpsModuleSourceRootType;
import org.jetbrains.kotlin.config.ResourceKotlinRootType;
import org.jetbrains.kotlin.config.SourceKotlinRootType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ModuleResUsegesAction extends ResUsegesAction {

    @Override
    protected void find(Project project, List<File2R> resRList, Module targetModule) {
        Set<JpsModuleSourceRootType<?>> sourceRootTypes = new HashSet<>(2);
        sourceRootTypes.add(JavaSourceRootType.SOURCE);
        sourceRootTypes.add(SourceKotlinRootType.INSTANCE);
        List<VirtualFile> virtualFileList = ModuleRootManager.getInstance(targetModule).getSourceRoots(sourceRootTypes);
        List<VirtualFile> findModuleFileList = new ArrayList<>();
        for (VirtualFile virtualFile : virtualFileList) {
            if (!isBuildSource(virtualFile.getPath())) {
                findModuleFileList.add(virtualFile);
            }
        }
        if (findModuleFileList.isEmpty()) {
            return;
        }
        ModuleResUsegesThread moduleResUsegesThread = new ModuleResUsegesThread(project, targetModule, resRList, findModuleFileList);
        ApplicationManager.getApplication().executeOnPooledThread(moduleResUsegesThread);
    }

    @Override
    protected boolean canUpdate(AnActionEvent e) {
        return Util.hasResDir(e);
    }
}
